<?php

namespace app\wx\controller;
use model\PolicyModel;
use think\Db;
use think\Exception;

/**
 * Desc: 业务员
 * Class SaleMan
 * Created By Doyi
 * Date: 2018/10/30
 */
class SaleMan extends CommonController
{
    public $request;

    public function __construct(\think\Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }

    /**
     * 首页
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if($this->request['type'] == 2){
            $where['insurant.delete_ts'] = 0;
            $where['insurant.status'] = ['not in',0];
            $info = Db::table('insurant')->where($where)->join('policy','policy.p_id=insurant.p_id','left')
                ->field('insurant.status,policy.created_at,policy.report_num,policy.policy_num')->order('insurant.status')
                ->select();
            foreach ($info as $k=>$v){
                $info[$k]['status_name'] = $this->remerStatus($info[$k]['status']);
            }
            $this->assign('info',$info);
            $this->assign('type',2);
        }else{
            $where['delete_ts'] = 0;
            $info = Db::table('policy')->where($where)->order('policy_status')->select();
            foreach ($info as $k=>$v){
                $info[$k]['status_name'] = $this->statusChangeWord($info[$k]['policy_status']);
            }
            $this->assign('info',$info);
            $this->assign('type',1);
        }
        $user = Db::table('user_info')->where('ui_id',$this->uid)->find();
        return $this->fetch('saleman/index',['user'=>$user]);
    }


    /**
     * 分发路由给物权人和保险人员
     */
    public function get_info()
    {
        if($this->request['type'] == 1){  //保险人员录入
            $this->redirect('SaleMan/get_policy_info',['policy_num'=>$this->request['policy_num']]);
        }else{      // 物权人
            $this->redirect('SaleMan/get_insurance_info',['policy_num'=>$this->request['policy_num']]);
        }
    }


    /**
     * 获取物权人编辑的信息
     */
    public function get_insurance_info()
    {
        $policy_num = $this->request['policy_num'];
        $policy = Db::table('policy')->where('policy_num',$policy_num)->find();
        $insurant = Db::table('insurant')->where('p_id',$policy['p_id'])->find();
        if($insurant['status'] == 1){
            $this->redirect('insurant/no_accept',['policy_num'=>$policy_num,'is_show'=>2]);
        }
        if($insurant['status'] == 2){
            $this->redirect('SaleMan/return_insurant',['policy_num'=>$policy_num,'is_show'=>2]);
        }
        if($insurant['status'] == 3){
            $this->redirect('insurant/no_accept',['policy_num'=>$policy_num,'is_show'=>2]);
        }
        if($insurant['status'] == 4){
            $this->redirect('insurant/no_accept',['policy_num'=>$policy_num,'is_show'=>2]);
        }
    }

    /**
     * 获取保单的详情信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_policy_info()
    {
        $policy_num = $this->request['policy_num'];
        $policy = Db::table('policy')->where('policy_num',$policy_num)->find();
        $info = Db::table('policy_item')->where('policy_num',$policy_num)->find();
        $info['policy_num'] = $policy['policy_num'];
        $info['report_num'] = $policy['report_num'];
        if($info['goods_type']){
            $info['goods_type_name'] = $this->getGoodType($info['goods_type']);
        }
        $info['status_name'] = $this->statusChangeWord($policy['policy_status']);
        if($policy['insurance_type']){
            $info['insurant_type_name'] = $this->getInsurantType($policy['insurance_type']);
        }
        if($info['present_img']){
            $info['present_img'] = json_decode($info['present_img']);
        }
        $info['recordor_id'] = $policy['recordor_id'];

        if($policy['recordor_id'] != 0){
            $user = Db::table('user_info')->where('ui_id',$policy['recordor_id'])->find();
            $recorder = Db::table('recorder_type')->where('id',$user['recoder_type'])->find();
            $user['type_name'] = $recorder['name'];
            $this->assign('user',$user);
        }
        if($policy['policy_status'] == 0){
            $where = [
                'status'=>1,
                'p_id'=>$policy['p_id']
            ];
            $return = Db::table('return_policy')->where($where)->select();
            $data = [];
            foreach ($return as $k=>$v){
                if($v['policy_return']){
                    $data['policy_return'][] = $v['policy_return'];
                }
                if($v['goods_return']){
                    $data['goods_return'][] = $v['goods_return'];
                }
                if($v['under_return']){
                    $data['under_return'][] = $v['under_return'];
                }
                if($v['situation_return']){
                    $data['situation_return'][] = $v['situation_return'];
                }
                if($v['info_return']){
                    $data['info_return'][] = $v['info_return'];
                }
                if($v['certificate_return']){
                    $data['certificate_return'][] = $v['certificate_return'];
                }
            }
            $this->assign('data',$data);
        }

        if($policy['policy_status'] == 4){
            $this->redirect('SaleMan/return_policy',['policy_num'=>$policy_num]);
        }
       return $this->fetch('saleman/detail_policy',['info'=>$info]);
    }


    /**
     * 驳回被保险人录入
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function return_insurant()
    {
        $policy_num = $this->request['policy_num'];
        $info = Db::table('policy')->where('policy_num',$policy_num)->field('policy_num,report_num,insurance_type,p_id')->find();
        $res = Db::table('insurant')->where('p_id',$info['p_id'])->find();
        $res['entrust_url'] = json_decode($res['entrust_url']);
        $info['insurance_type'] = $this->getInsurantType($info['insurance_type']);
        $res['status_name'] = $this->remerStatus($res['status']);
        $where = [
            'i_id'=>$res['i_id'],
            'status'=>0
        ];
        $return = Db::table('return_insurant')->where($where)->select();
        $data = [];
        foreach ($return as $k => $v){
            if(!empty($v['join_price'])){
                $data['join_price'][] = $v['join_price'];
            }
            if(!empty($v['look_goods_info'])){
                $data['look_goods_info'][] = $v['look_goods_info'];
            }
            if(!empty($v['bol_info'])){
                $data['bol_info'][] = $v['bol_info'];
            }
            if(!empty($v['receive_money'])){
                $data['receive_money'][] = $v['receive_money'];
            }
            if(!empty($v['auth_report'])){
                $data['auth_report'][] = $v['auth_report'];
            }
            $this->assign('data',$data);
        }
        return $this->fetch('saleman/return_insurant',['res'=>$res,'info'=>$info]);
    }



    /**
     * 修改驳回状态为待审核
     * @return \think\Response
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function return_save()
    {
        $i_id = $this->request['i_id'];
        $data = [
            'status'=>1,
        ];
        try{
            Db::startTrans();
            $where = [
                'auth_id'=>$this->uid,
                'form_table'=>'user_info',
                'i_id'=>$i_id
            ];
            Db::table('return_insurant')->where($where)->update($data);
            $conidtion = [
                'i_id'=>$i_id,
                'status'=>1,
            ];
            $res = Db::table('return_insurant')->where($conidtion)->select();
            if(!$res){
                Db::table('insurant')->where('i_id',$i_id)->update(['status'=>3]);
            }
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }


    /**
     * 驳回物权人的
     */
    public function getReturnMsg()
    {
        $type = $this->request['type'];
        $msg = $this->request['msg'];
        $data = [];
        if($type == 1){
            $data['join_price'] = $msg;
        }
        if($type == 2){
            $data['look_goods_info'] = $msg;
        }
        if($type == 3){
            $data['bol_info'] = $msg;
        }
        if($type == 4){
            $data['receive_money'] = $msg;
        }
        if($type == 5){
            $data['auth_report'] = $msg;
        }
        $p_id = $this->request['i_id'];
        $where = [
            'status'=>0,
            'auth_id'=>$this->uid,
            'form_table'=>'user_info',
            'i_id'=>$p_id
        ];
        $res = Db::table('return_insurant')->where($where)->find();
        if($res){
            $info = Db::table('return_insurant')->where($where)->update($data);
            if($info){
                return json(['code'=>200,'msg'=>'success']);
            }else{
                return json(['code'=>400,'msg'=>'error']);
            }
        }else{
            $data['i_id'] = $p_id;
            $data['auth_id'] = $this->uid;
            $data['form_table'] = 'user_info';
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['status'] = 0;
            $res = Db::table('return_insurant')->insert($data);
            if($res){
                return json(['code'=>200,'msg'=>'success']);
            }else{
                return json(['code'=>400,'msg'=>'error']);
            }
        }

    }

    /**
     * 物权人审核通过
     * @return \think\Response
     */
    public function return_commit()
    {
        $i_id = $this->request['i_id'];
        $data = [
            'auth_id'=>$this->uid,
            'i_id'=>$i_id,
            'form_table'=>'user_info',
            'status'=>1
        ];
        try{
            Db::startTrans();
            $res = Db::table('return_insurant')->where($data)->update(['status'=>2]);
            Db::table('insurant')->where('i_id',$this->request['i_id'])->update(['status' => 4]);
            Db::commit();
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }

    }



    /**
     * 业务员驳回保险人员界面
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function return_policy()
    {
        $policy_num = $this->request['policy_num'];
        $policy = Db::table('policy')->where('policy_num',$policy_num)->find();
        $info = Db::table('policy_item')->where('policy_num',$policy_num)->find();
        $info['p_id'] = $policy['p_id'];
        $info['policy_num'] = $policy['policy_num'];
        $info['present_img'] = json_decode($info['present_img']);
        $info['report_num'] = $policy['report_num'];
        $info['goods_type_name'] = $this->getGoodType($info['goods_type']);
        $info['status_name'] = $this->statusChangeWord($policy['policy_status']);
        $info['insurant_type_name'] = $this->getInsurantType($policy['insurance_type']);
        $info['recordor_id'] = $policy['recordor_id'];
        if($info['recordor_id'] != 0){
            $user = Db::table('user_info')->where('ui_id',$info['recordor_id'])->find();
            $recorder = Db::table('recorder_type')->where('id',$user['recoder_type'])->find();
            $user['type_name'] = $recorder['name'];
            $this->assign('user',$user);
        }

        $where = [
            'status'=>0,
            'p_id'=>$policy['p_id']
        ];
        $return = Db::table('return_policy')->where($where)->select();
        $data = [];
        foreach ($return as $k => $v){
            if(!empty($v['policy_return'])){
                $data['policy_return'][] = $v['policy_return'];
            }
            if(!empty($v['goods_return'])){
                $data['goods_return'][] = $v['goods_return'];
            }
            if(!empty($v['under_return'])){
                $data['under_return'][] = $v['under_return'];
            }
            if(!empty($v['situation_return'])){
                $data['situation_return'][] = $v['situation_return'];
            }
            if(!empty($v['info_return'])){
                $data['info_return'][] = $v['info_return'];
            }
            if(!empty($v['certificate_return'])){
                $data['certificate_return'][] = $v['certificate_return'];
            }
            $this->assign('data',$data);
        }

        return $this->fetch('saleman/return_policy',['info'=>$info]);
    }


    /**
     * 保存保险公司的编辑中的驳回
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function save_policy_msg()
    {
        $type = $this->request['type'];
        $msg = $this->request['msg'];
        $data = [];
        if($type == 1){
            $data['policy_return'] = $msg;
        }
        if($type == 2){
            $data['goods_return'] = $msg;
        }
        if($type == 3){
            $data['under_return'] = $msg;
        }
        if($type == 4){
            $data['situation_return'] = $msg;
        }
        if($type == 5){
            $data['info_return'] = $msg;
        }
        if($type == 6){
            $data['certificate_return'] = $msg;
        }
        $p_id = $this->request['p_id'];
        $where = [
            'status'=>0,
            'extend_id'=>$this->uid,
            'extend_table'=>'user_info',
            'p_id'=>$p_id
        ];
        $res = Db::table('return_policy')->where($where)->find();
        if($res){
            $info = Db::table('return_policy')->where($where)->update($data);
            if($info){
                return json(['code'=>200,'msg'=>'success']);
            }else{
                return json(['code'=>400,'msg'=>'error']);
            }
        }else{
            $data['p_id'] = $p_id;
            $data['extend_id'] = $this->uid;
            $data['extend_table'] = 'user_info';
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['status'] = 0;
            $res = Db::table('return_policy')->insert($data);
            if($res){
                return json(['code'=>200,'msg'=>'success']);
            }else{
                return json(['code'=>400,'msg'=>'error']);
            }
        }
    }


    /**
     * 驳回保险人员信息
     * @return \think\Response
     */
    public function return_policy_msg()
    {
        $p_id = $this->request['p_id'];
        $data = [
            'status'=>1,
        ];
        try{
            Db::startTrans();
            $where = [
                'extend_id'=>$this->uid,
                'extend_table'=>'user_info',
                'p_id'=>$p_id
            ];
            Db::table('return_policy')->where($where)->update($data);
            Db::table('policy')->where('p_id',$p_id)->update(['policy_status'=>0]);
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }

    /**
     * 保险通过
     * @return \think\Response
     */
    public function pass_policy()
    {
        $p_id = $this->request['p_id'];
        $data = [
            'extend_id'=>$this->uid,
            'p_id'=>$p_id,
            'extend_table'=>'user_info',
            'status'=>1
        ];
        try{
            Db::startTrans();
            Db::table('return_policy')->where($data)->update(['status'=>2]);
            Db::table('policy')->where('p_id',$this->request['p_id'])->update(['status' => 5]);
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }





    /*************************************************************************************************/
    /**
     * 业务员列表接口
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saleManIndex()
    {
        $insuranter = isset($this->request['type']) ? $this->request['type'] : 1;
        $PolicyModel = new PolicyModel();
        if($insuranter == 1){
            $info = $PolicyModel->where('delete_ts',0)->field('p_id,report_num,created_at,policy_status')->order('policy_status asc')->select();
        }else{
            $info = $PolicyModel->getInsurantInfo();
        }
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'']);
    }


    /**
     * 业务员点击查看保单详情 （物权人通用）
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPolicyOrInsurant()
    {
        $id = isset($this->request['p_id']) ? $this->request['p_id'] : '';
        $type = isset($this->request['type']) ? $this->request['type'] : 1;
        $PolicyModel = new PolicyModel();
        $res = $PolicyModel->where('p_id',$id)->find();
        if($type == 1){
            $info = Db::table('policy_item')->where('p_id',$id)->find();
            $info['policy_num'] = $res['policy_num'];
            $info['report_num'] = $res['report_num'];
            $info['insurance_type'] = $res['insurance_type'];
            $info['policy_status'] = $res['policy_status'];
            if($res['recordor_id'] == 0){
                $info['company_end_at'] = $res['company_end_at'];
            }else{
                $userInfo = Db::table('user_info')->where('ui_id',$res['recordor_id'])->find();
                $info['username'] = $userInfo['username'];
                $info['mobile'] = $userInfo['mobile'];
                $info['company_type'] = $userInfo['company_type'];
                $info['end_at'] = $res['company_type'];
            }
        }else{
            $info = Db::table('insurant')->where('p_id',$id)->find();
            $info['policy_num'] = $res['policy_num'];
            $info['report_num'] = $res['report_num'];
            $info['insurance_type'] = $res['insurance_type'];
            $info['policy_status'] = $res['policy_status'];
            $info['return_msg'] = Db::table('return_insurant')->where('p_id',$id)->find();
        }
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'']);

    }


    /**
     * 业务员驳回物权信息
     * @return \think\Response
     */
    public function returnInurant()
    {
        $data = [
            'join_price'=>isset($this->request['join_price']) ? $this->request['join_price'] : '',
            'look_goods_info'=>isset($this->request['look_goods_info']) ? $this->request['look_goods_info'] : '',
            'bol_info'=>isset($this->request['bol_info']) ? $this->request['bol_info'] : '',
            'receive_money'=>isset($this->request['receive_money']) ? $this->request['receive_money'] : '',
            'auth_report'=>isset($this->request['auth_report']) ? $this->request['auth_report'] : '',
            'auth_id'=>isset($this->request['auth_id']) ? $this->request['auth_id'] : '',
            'form_table'=>isset($this->request['form_table']) ? $this->request['form_table'] : '',
            'i_id'=>isset($this->request['i_id']) ? $this->request['i_id'] : '',
            'created_at'=>date('Y-m-d H:i:s',time()),
        ];
        try{
            Db::transaction();
            $res = Db::table('return_insurant')->insert($data);
            Db::table('insurant')->where('i_id',$this->request['i_id'])->update(['status' => 3]);
            Db::commit();
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
        }
    }

    /**
     * 业务员驳回保险人员
     * @return \think\Response
     */
    public function returnPolicy()
    {
        $data = [
            'insurance_type'=>isset($this->request['insurance_type']) ? $this->request['insurance_type'] : '',
            'good_type'=>isset($this->request['good_type']) ? $this->request['good_type'] : '',
            'damaged_goods'=>isset($this->request['damaged_goods']) ? $this->request['damaged_goods'] : '',
            'now_goods'=>isset($this->request['now_goods']) ? $this->request['now_goods'] : '',
            'rem_info'=>isset($this->request['rem_info']) ? $this->request['rem_info'] : '',
            'author_report'=>isset($this->request['author_report']) ? $this->request['author_report'] : '',
            'form_table'=>isset($this->request['form_table']) ? $this->request['form_table'] : '',
            'form_id'=>isset($this->request['form_id']) ? $this->request['form_id'] : '',
            'p_id'=>isset($this->request['p_id']) ? $this->request['p_id'] : '',
            'created_at'=>date('Y-m-d H:i:s',time()),
        ];
        try{
            Db::transaction();
            $res = Db::table('return_policy')->insert($data);
            Db::table('policy')->where('p_id',$this->request['p_id'])->update(['policy_status' => 0]);
            Db::commit();
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
        }
    }
}